package com.ronri.ams.task;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.ronri.ams.SystemEnvironmental;
import com.ronri.ams.bean.projectListbean;
import com.ronri.common.UrlEncode;
import com.ronri.common.WaitDialog;
import com.ronri.db.RemoteDataService;
import com.ronri.list.editview.ListEditAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件：ams.java
 * 包名：com.ronri.ams.task;
 * 说明：
 * 作者：MS During
 * 日期：2017/5/18
 * 历史：
 */
public class fragment_projectlist extends Fragment implements ListEditAdapter.Callback {

    private String _TAG = "fragment_projectlist";

    private List<Map<String, Object>> mData = new ArrayList<>();

    private ListView mListView = null;

    // 句柄
    private Context mContext = null;

    // 例行监测
    private int mCategory = 1;

    // 视图
    private View mView = null;

    private int mType = 0;

//    public fragment_projectlist(Context context, int single) {
//        mContext = context;
//        mCategory = single;
//    }

    public static fragment_projectlist newInstance(){
        fragment_projectlist fragment = new fragment_projectlist();
        return fragment ;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mType = getArguments().getInt("type");
        mCategory=getArguments().getInt("single");
        mContext=getActivity();
        Log.i("aaa","fragment_projectlist");
        // 如果视图存在，则直接返回
        if (mView == null){
            // 获取当前视图
            mView = inflater.inflate(R.layout.fragment_project_list, null);

            // 获取数据
            getRemoteData();
        }

        return mView;
    }

    @Override
    public void click(View v) {
        if (v != null)
        {
            TextView vTitle = (TextView) v.findViewById(R.id.patrol_title);
            Toast.makeText(getContext(), vTitle.getText(), Toast.LENGTH_SHORT);
        }
    }

    /**
     * 绑定数据源
     * @param lData
     */
    public void BindData(List<Map<String, Object>> lData)
    {
        mData = lData;
    }

    /****
     * 获取远程数据
     */
    public void getRemoteData() {

        RemoteDataService remoteDataService = new RemoteDataService();

        // 获取远程地址
        String szRemoteAddress = InitApplication.SystemConfig.getBase().RemoteAddress(
                InitApplication.SystemConfig.getModule().getPlist().getAddress());

//        Log.i("test","szRemoteAddress-->"+szRemoteAddress);
        Map<String, Object> mParam = new HashMap<String, Object>();

        try {
            mParam.put("addFrom", 1);
            mParam.put("addMark", "android");
            mParam.put("page", 1);
            mParam.put("pageSize", 5);
            mParam.put("user_id", InitApplication.LoginInfo.getData().getUser_id());
            mParam.put("detector_id", InitApplication.LoginInfo.getData().getDetectorid());
            mParam.put("category", mCategory);
        } catch (Exception ex) {

        }

        // 获取远程数据
        remoteDataService.getRemoteData(szRemoteAddress,
                UrlEncode.getUrlParamsByMap(mParam, false),
                InitApplication.SystemConfig.getModule().getLogin().getMethod(),
                new RemoteDataService.OnRemoteDataEvent() {
                    // 返回结果
                    @Override
                    public void OnResultEvent(String szString) {
                        if (szString.isEmpty()) {
                            ShowNoData(true);
                            return;
                        }

                        try {
                            projectListbean plist = JSON.parseObject(szString,  projectListbean.class);

                            if (plist.isSuccess()) {
                                if ( mView != null ){

                                    if(plist.getData().getRows().size() > 0)
                                        ShowNoData(false);
                                    else
                                        ShowNoData(true);

                                    // 获取列表
                                    mListView = (ListView)mView.findViewById(R.id.list);

                                    // 循环添加数据
                                    for(projectListbean.DataBean.RowsBean iTem : plist.getData().getRows()){
                                        Map<String, Object> ii = new HashMap<String, Object>();

                                        ii.put("tvProjectTitle", iTem.getName());
                                        ii.put("tvProjectRegion", iTem.getDept_name());
                                        ii.put("tvProjectDate", iTem.getStart_date());
                                        ii.put("tvProjectNature", iTem.getNature_name());
                                        ii.put("id", iTem.getProject_id());
                                        ii.put("natureid", iTem.getNature());

                                        mData.add(ii);
                                    }

                                    SimpleAdapter adapter = new SimpleAdapter(getContext(), mData,R.layout.v_project_element,
                                            new String[]{"tvProjectTitle","tvProjectRegion", "tvProjectDate", "tvProjectNature"},
                                            new int[]{R.id.tvProjectTitle,R.id.tvProjectRegion,
                                                    R.id.tvProjectDate,R.id.tvProjectNature});

                                    // 绑定数据源
                                    mListView.setAdapter(adapter);

                                    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                        @Override
                                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                            Map<String, Object> iTem = mData.get(i);

                                            if(iTem != null){
                                                if(mType == 1) {
                                                    Intent intent = new Intent(mContext, activity_Task_Detailed.class);
                                                    Bundle bundle = new Bundle();

                                                    bundle.putInt("type", mType);
                                                    bundle.putInt("project_id", (int) iTem.get("id"));
                                                    bundle.putInt("nature", (int) iTem.get("natureid"));
                                                    bundle.putString("dept_name", (String) iTem.get("tvProjectRegion"));
                                                    bundle.putString("end_date", (String) iTem.get("tvProjectDate"));
                                                    intent.putExtras(bundle);

                                                    // 启动详情
                                                    startActivity(intent);
                                                }else{
                                                    if (mType == 2)
                                                    {
                                                        Intent intent = new Intent(mContext, activity_Task_Detailed.class);
                                                        Bundle bundle = new Bundle();

                                                        bundle.putInt("type", mType);
                                                        bundle.putInt("project_id", (int) iTem.get("id"));
                                                        bundle.putInt("nature", (int) iTem.get("natureid"));
                                                        bundle.putString("dept_name", (String) iTem.get("tvProjectRegion"));
                                                        bundle.putString("end_date", (String) iTem.get("tvProjectDate"));
                                                        intent.putExtras(bundle);

                                                        // 启动详情
                                                        startActivity(intent);
                                                    }else{
                                                        Intent intent = new Intent(mContext, activity_Task_Detailed_List.class);
                                                        Bundle bundle = new Bundle();

                                                        bundle.putInt("type", mType);
                                                        bundle.putInt("project_id", (int) iTem.get("id"));
                                                        bundle.putInt("nature", (int) iTem.get("natureid"));
                                                        bundle.putString("dept_name", (String) iTem.get("tvProjectRegion"));
                                                        bundle.putString("end_date", (String) iTem.get("tvProjectDate"));
                                                        intent.putExtras(bundle);

                                                        // 启动详情
                                                        startActivity(intent);
                                                    }
                                                }
                                            }
                                        }
                                    });
                                }
                            } else {
                                // 提示错误信息
                            }
                        } catch (Exception ex) {

                        }
                    }

                    // 状态控制，是否显示加载过程
                    @Override
                    public void OnRuningState(int nStatus) {
                        if(nStatus == SystemEnvironmental.StartGetDataFromServer){
                            WaitDialog.ShowDialog(mContext, "请稍后...");
                        }else{
                            WaitDialog.HideDialog();
                        }
                    }

                    @Override
                    public String OnGetCustomParam() {
                        return null;
                    }
                }
        );
    }

    /***
     * 显示未找到数据
     * @param bShow
     */
    private void ShowNoData(boolean bShow){
        if (mView != null) {
            RelativeLayout rr = (RelativeLayout) mView.findViewById(R.id.vNoData);
            if (rr != null) {
                if (bShow == true) {
                    rr.setVisibility(View.VISIBLE);
                } else {
                    rr.setVisibility(View.GONE);
                }
            }
        }
    }
}
